package com.ruoyi.web.controller.union;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.union.domain.SysActivityRegistration;
import com.ruoyi.union.service.ISysActivityRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 活动报名Controller
 * 
 * @author ruoyi
 * @date 2025-01-15
 */
@RestController
@RequestMapping("/system/union/registration")
public class SysActivityRegistrationController extends BaseController
{
    @Autowired
    private ISysActivityRegistrationService sysActivityRegistrationService;

    /**
     * 查询活动报名列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysActivityRegistration sysActivityRegistration)
    {
        LambdaQueryWrapper<SysActivityRegistration> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotNull(sysActivityRegistration.getActivityId())) {
            wrapper.eq(SysActivityRegistration::getActivityId, sysActivityRegistration.getActivityId());
        }
        if (StringUtils.isNotNull(sysActivityRegistration.getMemberId())) {
            wrapper.eq(SysActivityRegistration::getMemberId, sysActivityRegistration.getMemberId());
        }
        if (StringUtils.isNotNull(sysActivityRegistration.getRegistrationStatus())) {
            wrapper.eq(SysActivityRegistration::getRegistrationStatus, sysActivityRegistration.getRegistrationStatus());
        }
        Page<SysActivityRegistration> pageReq = new Page<>(sysActivityRegistration.getPageNum(), sysActivityRegistration.getPageSize());
        Page<SysActivityRegistration> page = sysActivityRegistrationService.page(pageReq, wrapper);
        return new TableDataInfo(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }

    /**
     * 导出活动报名列表
     */
    @PreAuthorize("@ss.hasPermi('union:registration:export')")
    @Log(title = "活动报名", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysActivityRegistration sysActivityRegistration)
    {
        LambdaQueryWrapper<SysActivityRegistration> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotNull(sysActivityRegistration.getActivityId())) {
            wrapper.eq(SysActivityRegistration::getActivityId, sysActivityRegistration.getActivityId());
        }
        if (StringUtils.isNotNull(sysActivityRegistration.getMemberId())) {
            wrapper.eq(SysActivityRegistration::getMemberId, sysActivityRegistration.getMemberId());
        }
        if (StringUtils.isNotNull(sysActivityRegistration.getRegistrationStatus())) {
            wrapper.eq(SysActivityRegistration::getRegistrationStatus, sysActivityRegistration.getRegistrationStatus());
        }
        List<SysActivityRegistration> list = sysActivityRegistrationService.list(wrapper);
        ExcelUtil<SysActivityRegistration> util = new ExcelUtil<>(SysActivityRegistration.class);
        util.exportExcel(response, list, "活动报名数据");
    }

    /**
     * 获取活动报名详细信息
     */
    @GetMapping(value = "/{registrationId}")
    public AjaxResult getInfo(@PathVariable("registrationId") Long registrationId)
    {
        return success(sysActivityRegistrationService.selectSysActivityRegistrationByRegistrationId(registrationId));
    }

    /**
     * 新增活动报名
     */
    @PreAuthorize("@ss.hasPermi('union:registration:add')")
    @Log(title = "活动报名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysActivityRegistration sysActivityRegistration)
    {
        return toAjax(sysActivityRegistrationService.insertSysActivityRegistration(sysActivityRegistration));
    }

    /**
     * 修改活动报名
     */
    @PreAuthorize("@ss.hasPermi('union:registration:edit')")
    @Log(title = "活动报名", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysActivityRegistration sysActivityRegistration)
    {
        return toAjax(sysActivityRegistrationService.updateSysActivityRegistration(sysActivityRegistration));
    }

    /**
     * 删除活动报名
     */
    @PreAuthorize("@ss.hasPermi('union:registration:remove')")
    @Log(title = "活动报名", businessType = BusinessType.DELETE)
    @DeleteMapping("/{registrationIds}")
    public AjaxResult remove(@PathVariable Long[] registrationIds)
    {
        return toAjax(sysActivityRegistrationService.deleteSysActivityRegistrationByRegistrationIds(registrationIds));
    }

    /**
     * 会员报名活动
     */
    @PostMapping("/register")
    public AjaxResult registerActivity(@RequestBody SysActivityRegistration sysActivityRegistration)
    {
        boolean result = sysActivityRegistrationService.registerActivity(
            sysActivityRegistration.getActivityId(), 
            SecurityUtils.getUserId()
        );
        return result ? success("报名成功") : error("报名失败，可能已报名或活动不存在");
    }

    /**
     * 取消报名
     */
    @PostMapping("/cancel")
    public AjaxResult cancelRegistration(@RequestBody SysActivityRegistration sysActivityRegistration)
    {
        boolean result = sysActivityRegistrationService.cancelRegistration(
            sysActivityRegistration.getActivityId(), 
            SecurityUtils.getUserId()
        );
        return result ? success("取消报名成功") : error("取消报名失败");
    }

    /**
     * 根据活动ID查询报名列表
     */
    @GetMapping("/activity/{activityId}")
    public AjaxResult getRegistrationsByActivityId(@PathVariable Long activityId)
    {
        return success(sysActivityRegistrationService.selectSysActivityRegistrationByActivityId(activityId));
    }

    /**
     * 根据会员ID查询报名列表
     */
    @GetMapping("/member/{memberId}")
    public AjaxResult getRegistrationsByMemberId(@PathVariable Long memberId)
    {
        return success(sysActivityRegistrationService.selectSysActivityRegistrationByMemberId(memberId));
    }

    /**
     * 检查会员是否已报名该活动
     */
    @GetMapping("/check/{activityId}/{memberId}")
    public AjaxResult checkRegistrationExists(@PathVariable Long activityId, @PathVariable Long memberId)
    {
        boolean exists = sysActivityRegistrationService.checkRegistrationExists(activityId, memberId);
        return success(exists);
    }

    /**
     * 确认报名
     */
    @PostMapping("/confirm/{registrationId}")
    public AjaxResult confirmRegistration(@PathVariable Long registrationId)
    {
        boolean result = sysActivityRegistrationService.confirmRegistration(registrationId);
        return result ? success("确认报名成功") : error("确认报名失败");
    }

    /**
     * 根据活动ID统计报名人数
     */
    @GetMapping("/count/{activityId}")
    public AjaxResult countRegistrationsByActivityId(@PathVariable Long activityId)
    {
        int count = sysActivityRegistrationService.countRegistrationsByActivityId(activityId);
        return success(count);
    }
}

